package com.coffee.service.impl;

import com.coffee.common.BaseContext;
import com.coffee.common.CustomException;
import com.coffee.dao.CategoryDAO;
import com.coffee.dao.CoffeeDAO;
import com.coffee.entity.Category;
import com.coffee.entity.Coffee;
import com.coffee.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryDAO categoryDAO;

    @Autowired
    private CoffeeDAO coffeeDAO;
    @Override
    public List<Category> list() {
        return categoryDAO.list();
    }

    @Override
    public Category getById(String categoryId) {
        return categoryDAO.getById(categoryId);
    }

    @Override
    public void add(Category category) {
        //设置uuid
        category.setId(UUID.randomUUID().toString());
        //设置创建时间 创建人信息
        category.setCreateUser(BaseContext.get());
        category.setCreateTime(LocalDateTime.now());
        categoryDAO.add(category);
    }

    @Override
    public void edit(Category category) {
        //设置创建时间 创建人信息
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.get());
        categoryDAO.edit(category);
    }

    @Override
    public void delete(Category category) {
        List<Coffee> list = coffeeDAO.listCoffee(category.getId());
        if (list.size()!=0) {
            throw new CustomException("分类下有商品不允许删除");
        }
        categoryDAO.delete(category);
    }
}
